﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.CommandHandler.QingJia;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.Command.QingJia
{
    public class QingJiaCreateCommandHandler : IRequestHandler<QingJiaCreateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<LeaveApplicationForm> lRepo;
        private readonly IMapper mapper;
        private readonly IBaseRepository<RecordModel> rRepo;

        public QingJiaCreateCommandHandler(IBaseRepository<LeaveApplicationForm> lRepo,IMapper mapper,IBaseRepository<RecordModel>rRepo)
        {
            this.lRepo = lRepo;
            this.mapper = mapper;
            this.rRepo = rRepo;
        }



        public async Task<APIResult<object>> Handle(QingJiaCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<LeaveApplicationForm>(request);
            model.Status = Status.待审核;
            model.IsDeleted = false;
            model.CreateTime = DateTime.Now;
            var r   = lRepo.Add(model).Result;
            if(r>0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            RecordModel record = new RecordModel();
            record.FormId = model.FormId;   
            record.DepartId = request.DepartId;
            record.ShenPiName = "LS";
            record.ShenPiId = request.DepartName;
            record.Reason = request.LeaveReason;//请假原因
            record.ApprovalOpinion = "";//通过或拒绝
            record.Createtime = DateTime.Now;
            record.ApproveReson = "";//不通过后的意见
            record.Status = model.Status;
            record.HaoId = request.HaoId;
            await rRepo.Add(record);
            return await Task.FromResult(result); 
        }




    }
}
